package server.jdbcdao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import server.core.Checkpoint;
import server.dao.CheckPointDAO;
import server.idatasource.DataSourceFactory;
import server.idatasource.ICheckPointDataSource;

public class CheckpointJdbcDAO implements CheckPointDAO {

	/*Returns all checkpoints in a distance given by the argument.The arguments length and 
	 * width shows the users position. So the difference between a checkpoint's position and
	 * the given position should be less or equal to the distance. 
	 * If an error occurs, in the first elements username field 
	 * is returned "ERROR".*/
	@Override
	public List<Checkpoint> getNearestCheckpoints(int length, int width, int distance) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		ICheckPointDataSource checkpointData = ds.getCheckPointDataSource();

		List<List<String>> result = checkpointData.getNearestCheckpoints(length, width, distance);
		Iterator<List<String>> it1 = result.iterator();
		List<Checkpoint> checkpointList = new ArrayList<Checkpoint>();

		while (it1.hasNext()) {
			Iterator<String> it2 = it1.next().iterator();
			Checkpoint checkpoint = new Checkpoint();
			checkpoint.setId(Integer.parseInt(it2.next()));
			checkpoint.setWidth(Double.parseDouble(it2.next()));
			checkpoint.setLength(Double.parseDouble(it2.next()));
			checkpoint.setHeight(Double.parseDouble(it2.next()));
			checkpoint.setInformation(it2.next());
			checkpointList.add(checkpoint);
		}
		return checkpointList;
	}

	/*Inserts a new checkpoint given in the argument, into the database. Returns a string, with the
	 * value "OK" if the operation was successful, else returns "ERROR".*/
	@Override
	public String insertCheckpoint(Checkpoint checkpoint) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		ICheckPointDataSource checkpointData = ds.getCheckPointDataSource();

		return checkpointData.insertCheckpoint(checkpoint.getWidth(), checkpoint.getLength(), checkpoint.getHeight(), checkpoint.getInformation());
	}

	/*Deletes a checkpoint from database, having the username given in the argument. Returns a string, with the
	 * value "OK" if the operation was successful, else returns "ERROR".*/
	@Override
	public String deleteCheckpoint(int id) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		ICheckPointDataSource checkpointData = ds.getCheckPointDataSource();

		return checkpointData.deleteCheckpoint(id);
	}

	/*Updates the checkpoint given in the argument, in the database. Returns a string, with the
	 * value "OK" if the operation was successful, else returns "ERROR".*/
	@Override
	public String updateCheckpoint(Checkpoint checkpoint) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		ICheckPointDataSource checkpointData = ds.getCheckPointDataSource();

		return checkpointData.updateCheckpoint(checkpoint.getId(), checkpoint.getWidth(), checkpoint.getLength(), checkpoint.getHeight(), checkpoint.getInformation());
	}
}
